package com.lzh.others.collection.Map.Hashtable;

import java.util.*;

/**
 * 使用Hashtable的简单示例
 * <p>
 * Hashtable是线程安全的，可以在多线程环境中使用，它使用类哈希表的概念来实现字典操作
 */
public class DictionaryExample {
    public static void main(String[] args) {
        Dictionary<String, Integer> hashtable = new Hashtable<>();
        hashtable.put("one", 1);
        hashtable.put("two", 2);
        hashtable.put("three", 3);

        Integer value = hashtable.get("two");
        System.out.println("value = " + value);
        hashtable.remove("two");
        for (Enumeration<Integer> e = hashtable.elements(); e.hasMoreElements(); ) {
            System.out.println(e.nextElement());
        }

        Map<String, Integer> hashMap = new HashMap<>();
        hashMap.put("one", 1);
        hashMap.put("two", 2);
        hashMap.put("three", 3);

        //keySet()和values()支持remove,iterator.remove,collection.remove,retainAll和clear
        // 不支持add或addAll
        Collection<Integer> values = hashMap.values();
        Set<String> keys = hashMap.keySet();
        values.remove(3);
        keys.remove("two");
        for (Integer i : values) {
            System.out.println("i = " + i);
        }
        for (String key : keys) {
            System.out.println("key = " + key);
        }

        for (Map.Entry<String, Integer> stringIntegerEntry : hashMap.entrySet()) {
            System.out.println("stringIntegerEntry = " + stringIntegerEntry.getValue());
        }

    }
}
